package com.zhangmeng.controller;

import com.zhangmeng.service.impl.ServerImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author zm
 * @date 2025/2/22 11:11
 * @version: 1.0
 */
@RestController
@RequestMapping("/tcpSocket")
public class TcpSocketController {

    @RequestMapping("/send")
    public void send(String message,String key){
        Socket socket = ServerImpl.socketList.get(key);
        if(socket!= null){
            try {
                BufferedWriter  writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                writer.write(message);
                writer.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
